<template>
	<view class="body">
		<view class="head">
			<view class="head_left" @tap="back()">
				<image src="../../../static/wode/return.png" mode=""></image>
				<text>返回</text>
			</view>
			<view class="head_mid">
				编辑信息
			</view>
			<view class="head_right">

			</view>
		</view>
		<view class="body_top" @tap="gettoken('img')">
			<image v-if="userHeadImg!=''" class="headImg" :src="userHeadImg"></image>
			<image v-else class="headImg" :src="myInfo.avatar" mode=""></image>

			<view class="body_item_right">
				<text class="rightText">编辑头像</text>
				<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
			</view>
		</view>
		<view class="body_mid">
			<view class="body_mid_item" @tap="toNickName">
				<text>昵称</text>
				<view class="body_item_right">
					<text class="rightText">{{nick_name}}</text>
					<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
				</view>
			</view>
			<view class="body_mid_item">
				<text>出生日期</text>
				<view class="body_item_right">
					<picker class="input rightText" mode="date" start="1971-01-01" :end="endDate" @change="bindDateChange">
						<view class="uni-input">{{goAbroadDate}}</view>
					</picker>
					<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
				</view>
			</view>
			<view class="body_mid_item" @tap="selsectAddress">
				<text>所在地</text>
				<view class="body_item_right">
					<text class="rightText">{{suozaidi}}</text>
					<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
				</view>
			</view>
			<view class="body_mid_item" @tap="selsectNativePlace">
				<text>故乡</text>
				<view class="body_item_right">
					<text class="rightText">{{jiguan}}</text>
					<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
				</view>
			</view>
			<view class="body_mid_item noboder" @tap="toHobby">
				<text>个人偏好</text>
				<view class="body_item_right">
					<text class="rightText">{{myInfo.preference}}</text>
					<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="body_bottom">
			<view class="bottom_item marginbox">
				<view class="" v-if="companyInfo!=''">
					<view class="body_mid_item" v-for="(item,index) in companyInfo" :key="index" @tap="toChangeCompany(item.id)">
						<text>{{item.company_name}}</text>
						<view class="body_item_right">
							<text class="rightText">{{item.start_time}}-{{item.end_time}}</text>
							<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
						</view>
					</view>
				</view>
				<view class="bottom_item_mid" @tap="tocompany">
					<image src="../../../static/wode/tianjia.png" mode=""></image>
					<text>公司经历</text>
				</view>
			</view>
			<view class="bottom_item noboder">
				<view class="" v-if="schoolInfo!=''">
					<view class="body_mid_item" v-for="(item,index) in schoolInfo" :key="index" @tap="toChangeSchool(item.id)">
						<text>{{item.school_name}}</text>
						<view class="body_item_right">
							<text class="rightText">{{item.start_time}}-{{item.end_time}}</text>
							<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
						</view>
					</view>
				</view>
				<view class="bottom_item_mid" @tap="toschool">
					<image src="../../../static/wode/tianjia.png" mode=""></image>
					<text>学校经历</text>
				</view>
			</view>
		</view>
		<view class="body_last" @tap="erweima">
			<text>我的二维码</text>
			<image class="rightImg" src="../../../static/wode/arrow_right.png" mode=""></image>
		</view>
	</view>
</template>

<script>
	import qiniu from "../../../common/qiniuUploader.js"
	import webim from "../../../common/webim_wx.min.js"
	import {
		mapState
	} from 'vuex'

	export default {
		computed: mapState(['Login']),
		data() {
			return {
				goAbroadDate: "请选择",
				endDate: "", //当前日期
				imgList: "",
				userHeadImg: "", //用户头像
				myInfo: "",
				user_id: "", //登录用户id
				user_info: "", //用户信息
				nick_name: '',
				city: '', //城市
				country: '', //国家
				companyInfo: '', //公司信息
				schoolInfo: '', //学校信息
				suozaidi: '', //所在地
				jiguan: '', //籍贯
				hometown: '', //故乡
			};
		},
		onLoad() {

		},
		method() {
			var myDate = new Date(); //获取系统当前时间
			this.endDate = myDate.toLocaleDateString();
		},
		onShow() {
			this.nick_name = uni.getStorageSync('nick_name')
			this.user_id = uni.getStorageSync('user_id');
			this.user_info = uni.getStorageSync('user_info');
			this.getUserInfo()
			this.getCompany()
			this.getSchool()
			this.getaddres(); //获取地名
		},
		onHide() {
			uni.setStorageSync('user_info', this.myInfo);
			console.log(1)
		},
		methods: {
			// #ifdef APP-PLUS
			gettoken(type) {
				this.appselectImg(type)
			},

			// #endif
			// #ifdef MP-WEIXIN
			gettoken(type) {
				var me = this;
				var data = {
					key: null,
				}
				this.myAjaxNewPost({
					model: 'push',
					controllerName: 'upload',
					actionName: 'getToken',
					data: data,
					successBack: function(res) {

						me.token = res.data.data
						console.log(me.token)
						if (me.token != '') {
							me.selectImg(type)
						}

					}
				})
			},
			// #endif
			appselectImg(type) {
				var me = this;
				uni.showActionSheet({
					itemList: ['拍照', '相册'],
					itemColor: '#008AFF',
					success: function(res) {
						var index = res.tapIndex;
						if (index == 1) {
							uni.chooseImage({
								count: 1,
								sizeType: ['compressed'],
								sourceType: ['album'],
								success: function(res) {
									// me.card_front = res.tempFilePaths[0];
									var filePath = res.tempFilePaths[0];
									uni.uploadFile({
										url: 'http://www.curiousmore.com:8768/eg-api/push/upload/uploadPicture',
										// url: 'http://192.168.101.14:8768/eg-api/push/upload/uploadPicture',
										filePath: filePath,
										name: 'file',
										formData: {
											'user_id': uni.getStorageSync('user_id'),
											'type': 'pic'
										},
										success: function(uploadFileRes) {
											// me.backImgArr.push(JSON.parse(uploadFileRes.data)[0]);
											console.log(JSON.parse(uploadFileRes.data)[0]);
											var imageURL = JSON.parse(uploadFileRes.data)[0];
											me.userHeadImg = imageURL
											me.setHeadImg()

										},
										fail: function(e) {
											uni.showToast({
												title: '图片上传失败，请重试',
												icon: 'none'
											})
											return;
										}
									});
								}
							});
						} else if (index == 0) {
							uni.chooseImage({
								count: 1,
								sizeType: ['compressed'],
								sourceType: ['camera'],
								success: function(res) {
									// me.card_front = res.tempFilePaths[0];

									var filePath = res.tempFilePaths[0];
									uni.uploadFile({
										url: 'http://www.curiousmore.com:8768/eg-api/push/upload/uploadPicture',
										// url: 'http://192.168.101.14:8768/eg-api/push/upload/uploadPicture',
										filePath: filePath,
										name: 'file',
										formData: {
											'user_id': uni.getStorageSync('user_id'),
											'type': 'pic'
										},
										success: function(uploadFileRes) {
											// me.backImgArr.push(JSON.parse(uploadFileRes.data)[0]);
											console.log(JSON.parse(uploadFileRes.data)[0]);
											var imageURL = JSON.parse(uploadFileRes.data)[0];
											me.userHeadImg = imageURL
											me.setHeadImg()

										},
										fail: function(e) {
											uni.showToast({
												title: '图片上传失败，请重试',
												icon: 'none'
											})
											return;
										}
									});
								}
							});
						}
					}
				});
			},
			selectImg(type) { //上传头像
				var me = this;
				uni.showActionSheet({
					itemList: ['拍照', '相册'],
					itemColor: '#008AFF',
					success: function(res) {
						var index = res.tapIndex;
						if (index == 1) {
							uni.chooseImage({
								count: 1,
								sizeType: ['compressed'],
								sourceType: ['album'],
								success: function(res) {
									// me.card_front = res.tempFilePaths[0];
									var filePath = res.tempFilePaths[0];
									// 交给七牛上传
									uni.showLoading({
										title: '上传中'
									})
									if (me.token != '') {
										qiniu.upload(filePath, (res) => {
											console.log(res.imageURL)
											var imageURL = res.imageURL

											me.userHeadImg = imageURL
											me.setHeadImg()
											uni.hideLoading()
											console.log('file url is: ' + res.fileUrl);
										}, (error) => {
											console.log('error: ' + error);
											uni.showToast({
												title: '上传失败',
												icon: null
											});
										}, {
											region: 'ECN',
											domain: 'http://img.curiousmore.com', // // bucket 域名，下载资源时用到。如果设置，会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字段。否则需要自己拼接
											key: null, // [非必须]自定义文件 key。如果不设置，默认为使用微信小程序 API 的临时文件名
											// 以下方法三选一即可，优先级为：uptoken > uptokenURL > uptokenFunc
											uptoken: me.token, // 由其他程序生成七牛 uptoken
											// uptokenURL: 'http://192.168.6.6:8768/eg-api/push/upload/getToken', // 从指定 url 通过 HTTP GET 获取 uptoken，返回的格式必须是 json 且包含 uptoken 字段，例如： {"uptoken": "[yourTokenString]"}
											uptokenFunc: function() {
												return '[yourTokenString]';
											}
										}, (res) => {
											console.log('上传进度', res.progress)
											console.log('已经上传的数据长度', res.totalBytesSent)
											console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)


										}, () => {
											// 取消上传
										}, () => {

											// `before` 上传前执行的操作
										}, (err) => {


											// `complete` 上传接受后执行的操作(无论成功还是失败都执行)
										});
									}
								}
							});
						} else if (index == 0) {
							uni.chooseImage({
								count: 1,
								sizeType: ['compressed'],
								sourceType: ['camera'],
								success: function(res) {
									// me.card_front = res.tempFilePaths[0];
									var filePath = res.tempFilePaths[0];
									// 交给七牛上传
									uni.showLoading({
										title: '上传中'
									})
									if (me.token != '') {
										qiniu.upload(filePath, (res) => {
											console.log(res.imageURL)
											var imageURL = res.imageURL

											me.userHeadImg = imageURL
											me.setHeadImg()
											uni.hideLoading()
											console.log('file url is: ' + res.fileUrl);
										}, (error) => {
											uni.showToast({
												title: '上传失败',
												icon: null
											});
											console.log('error: ' + error);
										}, {
											region: 'ECN',
											domain: 'http://img.curiousmore.com', // // bucket 域名，下载资源时用到。如果设置，会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字段。否则需要自己拼接
											key: null, // [非必须]自定义文件 key。如果不设置，默认为使用微信小程序 API 的临时文件名
											// 以下方法三选一即可，优先级为：uptoken > uptokenURL > uptokenFunc
											uptoken: me.token, // 由其他程序生成七牛 uptoken
											// uptokenURL: 'http://192.168.6.6:8768/eg-api/push/upload/getToken', // 从指定 url 通过 HTTP GET 获取 uptoken，返回的格式必须是 json 且包含 uptoken 字段，例如： {"uptoken": "[yourTokenString]"}
											uptokenFunc: function() {
												return '[yourTokenString]';
											}
										}, (res) => {
											console.log('上传进度', res.progress)
											console.log('已经上传的数据长度', res.totalBytesSent)
											console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)

										}, () => {
											// 取消上传
										}, () => {
											// `before` 上传前执行的操作
										}, (err) => {


											// `complete` 上传接受后执行的操作(无论成功还是失败都执行)
										});
									}
								}
							});
						}
					}
				});
			},
			setHeadImg() {
				var me = this;
				var data = {
					user_id: uni.getStorageSync('user_id'),
					avatar: me.userHeadImg
				}
				this.myAjaxNewPost({
					model: 'user',
					controllerName: 'userInfo',
					actionName: 'update',
					data: data,
					successBack: function(res) {
						console.log(res)
						if (res.data.code == 0) {
							uni.showToast({
								title: '上传成功',
								icon: "none"
							});
							me.getUserInfo()
							me.setProfilePortrait()
						}

					}
				})
			},
			setProfilePortrait() {//设置腾讯云头像
				var profile_item = [{
						"Tag": "Tag_Profile_IM_Image",
						"Value": this.userHeadImg
					}
				];
				var options = {
					'ProfileItem': profile_item
				};

				webim.setProfilePortrait(
					options,
					function(resp) {
						
					},
					function(err) {
						console.log(err.ErrorInfo);
					}
				);
			},
			getaddres() {
				var countryName = uni.getStorageSync('countryName');
				var province = uni.getStorageSync('province');
				var city1 = uni.getStorageSync('city');
				var guojia = uni.getStorageSync('guojia');
				var shengfen = uni.getStorageSync('shengfen');
				var chengshi = uni.getStorageSync('chengshi');
				var isChoose = uni.getStorageSync('isChoose');
				if (this.country != '') {
					this.suozaidi = this.country
				} else if (this.country != '' && this.city != '') {
					this.suozaidi = this.country + ' · ' + this.city
				} else {
					this.suozaidi = '未知'
				}

				if (chengshi != '' && shengfen != '') {
					this.suozaidi = guojia + ' · ' + chengshi
				} else if (chengshi == '' && shengfen != '') {
					this.suozaidi = guojia + ' · ' + shengfen
				} else {
					this.suozaidi = guojia
				}
				if (this.hometown != '') {
					this.jiguan = this.hometown
				} else {
					this.jiguan = '未知'
				}
				if (city1 != '' && province != '') {
					this.jiguan = countryName + ' · ' + city1
				} else if (city1 == '' && province != '') {
					this.jiguan = countryName + ' · ' + province
				} else {
					this.jiguan = countryName
				}
			},
			getCompany() { //获取公司信息
				var me = this;
				var data = {
					user_id: uni.getStorageSync('user_id')
				}
				this.myAjaxNew({
					model: 'user',
					controllerName: 'userCompany',
					actionName: 'query',
					data: data,
					successBack: function(res) {
						console.log(res)
						me.companyInfo = res.data.data
						console.log(me.companyInfo)
					}
				})
			},
			getSchool() {
				var me = this;
				var data = {
					user_id: uni.getStorageSync('user_id')
				}
				this.myAjaxNew({
					model: 'user',
					controllerName: 'userSchool',
					actionName: 'query',
					data: data,
					successBack: function(res) {
						console.log(res)
						me.schoolInfo = res.data.data
						console.log(me.schoolInfo)
					}
				})
			},
			getUserInfo() { //获取用户信息
				var me = this;
				var data = {
					id: uni.getStorageSync('user_id')
				}
				this.myAjaxNew({
					model: 'user',
					controllerName: 'userInfo',
					actionName: 'findById',
					data: data,
					successBack: function(res) {
						me.myInfo = res.data.data
						console.log(me.myInfo, '用户详情')
						me.city = me.myInfo.city;
						me.country = me.myInfo.country;
						me.hometown = me.myInfo.hometown;
						var arr = me.myInfo.birth.split('T') //用户生日日期
						me.goAbroadDate = arr[0]
						uni.setStorageSync('user_info', me.myInfo)
					}
				})
			},
			// 			selectUserInfor() {
			// 				var data = {
			// 					user_id: this.user_id,
			// 				};
			// 				var me = this;
			// 				this.myAjaxNew({
			// 					model: 'user',
			// 					controllerName: 'userInfo',
			// 					actionName: 'query',
			// 					data: data,
			// 					successBack: function(res) {
			// 						console.log(res);
			// 						this.user_Info = res.data[0]
			// 						uni.showToast({
			// 							title: res.data.message,
			// 							icon: 'none'
			// 						})
			// 					},
			// 				})
			// 			},
			bindDateChange(e) {
				this.goAbroadDate = e.target.value
				var data = {
					user_id: this.user_id,
					birth: this.goAbroadDate
				}
				this.myAjaxNewPost({
					model: 'user',
					controllerName: 'userInfo',
					actionName: 'update',
					data: data,
					successBack: function(res) {
						console.log(res)
						if (res.data.code == 0) {
							uni.showToast({
								title: '修改成功',
								icon: "none"
							});
						}
					}
				})
			},
			toNickName() {
				uni.navigateTo({
					url: "../nickname/nickname"
				})
			},
			toHobby() {
				uni.navigateTo({
					url: '../hobby/hobby?user_id=' + this.myInfo.user_id + '&type=back'
				})
			},
			erweima() {
				uni.navigateTo({
					url: '../erweima/erweima?user_id=' + this.user_id
				})
			},
			toschool() {
				uni.navigateTo({
					url: '../school/school?user_id=' + this.user_id
				})
			},
			toChangeSchool(id) {
				uni.navigateTo({
					url: '../school/school?user_id=' + this.user_id + '&recordId=' + id
				})
			},
			tocompany() {
				uni.navigateTo({
					url: '../company/company?user_id=' + this.user_id
				})
			},
			toChangeCompany(id) { //修改公司经历
				uni.navigateTo({
					url: '../company/company?user_id=' + this.user_id + '&recordId=' + id
				})
			},
			selsectAddress() { //选择所在城市
				uni.navigateTo({
					url: '../countryLists/countryLists?isNativePlace=false'
				})
			},
			selsectNativePlace() { //选择籍贯
				uni.navigateTo({
					url: '../countryLists/countryLists?isNativePlace=true&isHometown=true'
				})
			},
			back() { //返回
				uni.navigateBack({
					delta: 1
				})
			},
		}
	}
</script>

<style scoped>
	.head {
		width: 100%;
		display: flex;
		justify-content: space-between;
		height: 128upx;
		background: #ffffff;
		padding: 0 24upx;
		z-index: 200;
	}

	.head image {
		width: 20upx;
		height: 36upx;
		margin-right: 9upx;
	}

	.head_left {
		height: 82upx;
		width: 106upx;
		display: flex;
		align-items: center;
		line-height: 82upx;
		color: #666666;
		font-size: 32upx;

	}

	.head_right {
		width: 102upx;
	}

	.body {
		font-size: 32upx;
	}

	.body_top {
		background: #ffffff;
		padding: 16upx 24upx;
		margin-top: 20upx;
		display: flex;
		justify-content: space-between;
	}

	.headImg {
		width: 128upx;
		height: 128upx;
		border-radius: 50%;
		overflow: hidden;
	}

	.rightImg {
		width: 16upx;
		height: 28upx;
		margin-left: 30upx;
	}

	.rightText {
		color: #999999;
		/* width: 70%; */
		max-width: 440upx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap
	}

	.body_item_right {
		display: flex;
		align-items: center;
	}

	.body_mid {
		margin-top: 20upx;
	}

	.body_mid_item {
		background: #ffffff;
		display: flex;
		justify-content: space-between;
		padding: 24upx 24upx;
		border-bottom: 1px solid #e6e6e6;
	}

	.body_bottom {
		margin-top: 20upx;
	}

	.bottom_item {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		border-bottom: 1upx solid #e6e6e6;
	}

	.bottom_item_mid {
		color: #46CECF;
		line-height: 88upx;
		text-align: center;
		font-size: 32upx;
	}

	.bottom_item_mid image {
		width: 26upx;
		height: 26upx;
		margin-right: 10upx;
	}

	.body_last {
		margin-top: 20upx;
		background: #ffffff;
		display: flex;
		justify-content: space-between;
		padding: 24upx 28upx;
	}

	.marginbox {
		margin-bottom: 20upx;
	}

	.noboder {
		border: none;
	}
</style>
